package com.xing.test;

import com.alibaba.fastjson.JSON;
import com.xlj.pojo.User;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.*;

public class ceshiRedis {

    @Test
    public void measureset() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(0);//选择对应的数据库
        jedis.set("bobo", "曾经是一个组合，参加过快乐男孩");
        String bobo = jedis.get("bobo");
        System.out.println(bobo);
    }

    @Test
    public void measurehset() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(0);//选择对应的数据库
        jedis.hset("team", "name", "洛杉矶湖人");
        jedis.hset("team", "count", "13");
        jedis.hset("team", "opponent", "休斯顿火箭队");
        Map<String, String> team = jedis.hgetAll("team");
        for (Map.Entry<String, String> entry : team.entrySet()) {
            System.out.println(entry.getKey() + "====" + entry.getValue());
        }
    }

    @Test
    public void measure1() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(0);//选择对应的数据库
        jedis.lpush("english", "money", "gold", "bussiness", "itcompany", "fashion");
        List<String> english = jedis.lrange("english", 2, 3);
        Iterator<String> iterator1 = english.iterator();
//        while (iterator1.hasNext()){
//            String next = iterator1.next();
//            System.out.println(next);
//        }
        for (String ss : english) {
            System.out.println(ss);
        }
    }

    @Test
    public void measure2() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(0);//选择对应的数据库
        Set<String> team = jedis.smembers("bobos");
        for (String ss : team) {
            System.out.println(ss);
        }
    }

    @Test
    public void measure_zset() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(0);//选择对应的数据库
        jedis.zadd("city", 0, "拉斯维加斯");
        jedis.zadd("city", 1, "达拉斯");
        jedis.zadd("city", 1, "北京");
        jedis.zadd("city", 2, "大上海-魔都");
        jedis.zadd("city", 2, "大上海-魔都");
        Set<String> city = jedis.zrange("city", 0, 4);
        for (String c : city) {
            System.out.println(c);
        }
    }

    @Test
    public void measure_json() {
        //JSON 对象/数组/转换字符串
        User user = new User();
        User user1 = new User();
        User user2 = new User();
        user.setUserName("铁林");
        user.setAddress("辽宁铁岭");
        user.setPhone("122");
        user1.setUserName("威威");
        user1.setAddress("甘肃武威市");
        user1.setPhone("136");
        user2.setUserName("大理子");
        user2.setAddress("云南省大理市");
        user2.setPhone("199");
        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user1);
        list.add(user2);
        String s = JSON.toJSONString(user);
//        System.out.println(s);
        String string = JSON.toJSONString(list);
        System.out.println(string);

    }

    @Test
    public void measure_zjs() {
    //        json 字符串转换为对象
        String s = "{\"address\":\"辽宁铁岭\",\"id\":0,\"phone\":\"122\",\"userName\":\"铁林\"}";

        User user = JSON.parseObject(s, User.class);
        //System.out.println(user);
        String shu = "[{\"address\":\"辽宁铁岭\",\"id\":0,\"phone\":\"122\",\"userName\":\"铁林\"},{\"address\":\"甘肃武威市\",\"id\":0,\"phone\":\"136\",\"userName\":\"威威\"}" +
                ",{\"address\":\"云南省大理市\",\"id\":0,\"phone\":\"199\",\"userName\":\"大理子\"}]";
        List<User> list = JSON.parseArray(shu, User.class);
        Iterator<User> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

    }

}
